package com.helios.user.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.helios.core.bean.Result;
import com.helios.user.bean.Manager;
import com.helios.user.service.ManagerService;

/**
 * 管理员相关的控制器.
 * 
 * @author liyu
 */
@RestController
@RequestMapping("manager")
public class ManagerController {
	
	@Autowired
	private ManagerService managerService;
	
	/**
	 * 管理员信息列表.
	 */
	@RequestMapping(value = "list.html")
	public ModelAndView toList() {
		ModelAndView modelAndView = new ModelAndView("user/manager_list");
		return modelAndView;
	}
	
	@RequestMapping(value = "list.json")
	public Result getList(HttpServletRequest request) {
		Result result = new Result();
		
		int page      = NumberUtils.toInt(request.getParameter("page"), 1);
		int rows      = NumberUtils.toInt(request.getParameter("rows"), 10);
		
		int count = managerService.getCount();
		List<Manager> beans = managerService.gets(page, rows);
		
		result.putMessage("beans", beans);
		result.putMessage("page", page);
		result.putMessage("count", count);
		
		return result;
	}

    /**
     * 管理员个人设置页面. 
     */
    @RequestMapping(value = "profile.html")
    public ModelAndView toProfile(HttpServletRequest request){
        ModelAndView modelAndView = new ModelAndView("user/manager_profile");
        modelAndView.addObject("admin", request.getSession().getAttribute("KEY_AUTHOR_SESSION"));
        return modelAndView;
    }
    
    /**
     * 修改管理员信息. 
     * 
     * 参数:
     * username 用户名
     * realname 姓名
     * password 密码
     * image    头像
     */
    @RequestMapping(value = "update.json")
    public Result update(HttpServletRequest request){
        Result result = new Result();
        
        /* 获取当前登陆的用户*/
        Manager manager = (Manager) request.getSession().getAttribute("KEY_AUTHOR_SESSION");
        if(manager == null){
            result.setCode(-1);
            result.putInfo("登陆失效,请刷新页面!");
            return result;
        }
        
        String username = request.getParameter("username");
        String realname = request.getParameter("realname");
        String password = request.getParameter("password");
        String picture = request.getParameter("picture");
        
        if(StringUtils.trimToNull(username) == null){
            result.setCode(-1);
            result.putInfo("用户名不能为空!");
            return result;
        }
        
        if(StringUtils.trimToNull(realname) == null){
            result.setCode(-1);
            result.putInfo("真实姓名不能为空!");
            return result;
        }
        
        if(StringUtils.trimToNull(password) != null){
            manager.setPassword(password);
        }
        
        manager.setUsername(username);
        manager.setRealname(realname);
        manager.setPicture(picture);
        
        boolean bol = false;//managerService.update(manager);
        if(bol){
            result.setCode(0);
            result.putInfo("");
        }else{
            result.setCode(-1);
            result.putInfo("提交失败");
        }
        return result;
    }
}
